#!/bin/sh

die() {
    echo "$1"
    exit 1
}

# Check linux distribution
cat /etc/centos-release | grep "CentOS release 6" > /dev/null  || die "Only centos 6.x is supported."

# Disable selinux if it is enabled.
selinuxenabled && setenforce 0

# Install EPEL repo if it is not available
[ -f /etc/yum.repos.d/epel.repo ] || rpm -Uvh http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm || die "Cannot install EPEL repo." 

# Install rpmfusion repo if it is not available
[ -f /etc/yum.repos.d/rpmfusion-free-updates.repo ] || rpm -Uvh http://download1.rpmfusion.org/free/el/updates/6/i386/rpmfusion-free-release-6-1.noarch.rpm || die "Cannot install RPMFusion repo."

# Install python 
yum install -y python python-devel python-pip python-setuptools || die "Cannot install python tools."

# Install prerequisite python modules
yum install -y python-imaging python-magic python-mutagen python-webpy || die "Cannot install prerequisite python modules."

# Install ffmpeg
yum install -y ffmpeg ffmpeg-devel gcc || die "Cannot install ffmpeg."

# Install ffvideo
pip show FFVideo|grep "FFVideo" > /dev/null ||  easy_install ffvideo

# Install lighttpd as well as its requires
yum install -y lighttpd lighttpd-fastcgi python-flup || die "Cannot install lighttpd."  

# Start lighttpd automatically 
chkconfig lighttpd on

# Initialize dir permissions for lighttpd
[ -d data ] || mkdir -p data/sessions
[ -d static/uploads ] || mkdir -p static/uploads
chown -R lighttpd:lighttpd data
chown -R lighttpd:lighttpd static

# Apply default lighttpd config 
cp -rf /etc/lighttpd /etc/lighttpd.bak
cp -rf tools/lighttpd/* /etc/lighttpd

echo 
echo 
echo "Install successfully!"
echo 
echo "PLEASE MOVE phonecms TO /var/www/webapps and RESTART lighttpd."
echo
echo "VISIT http://SERER_ADDR:9000 for your site!"
echo 
echo
